package com.twxm.breed.util;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class GetTableUtil {
    private XPath xpath;
    public static ArrayList<String> getTable(String tagName){
        try {
            //读取配置文件
            InputStream inputStream = GetTableUtil.class.getClassLoader().getResourceAsStream("excel/dissolved_oxygen_excel.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(inputStream);

            //解析配置文件
            //Water?Weather?..
            NodeList nodes = doc.getElementsByTagName(tagName);
            ArrayList<String> fieldList = new ArrayList<>();
            //遍历nodes，虽然只有一个
            for (int i = 0; i < nodes.getLength(); ++i){
                Node tagNode = nodes.item(i);
                NodeList fieldNodes = tagNode.getChildNodes();
                //遍历所有的field
                for (int j = 0; j < fieldNodes.getLength(); ++j) {
                    Node fieldNode = fieldNodes.item(j);
                    if (fieldNode.getNodeType() == Node.ELEMENT_NODE && fieldNode.hasAttributes()) {
                        NamedNodeMap attributes = fieldNode.getAttributes();
                        Node labelNode = attributes.getNamedItem("label");
                        if (labelNode != null) {
                            String label = labelNode.getNodeValue();
                            fieldList.add(label);
                        }
                    }
                }
            }
            return fieldList;
        }
        catch (SAXException | IOException | ParserConfigurationException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getName(String name) throws Exception {
        InputStream inputStream = GetTableUtil.class.getClassLoader().getResourceAsStream("excel/dissolved_oxygen_excel.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputStream);

        XPathFactory xPathfactory = XPathFactory.newInstance();
        this.xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("//field[@label='" + name + "']/@name");
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        if (node != null) {
            return node.getNodeValue();
        } else {
            throw new IllegalArgumentException("Property not found: " + name);
        }
    }

    public String getType(String name) throws Exception {
        InputStream inputStream = GetTableUtil.class.getClassLoader().getResourceAsStream("excel/dissolved_oxygen_excel.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputStream);

        XPathFactory xPathfactory = XPathFactory.newInstance();
        this.xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("//field[@label='" + name + "']/@type");
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        if (node != null) {
            return node.getNodeValue();
        } else {
            throw new IllegalArgumentException("Property not found: " + name);
        }
    }

    public static void main(String[] args) throws Exception {
        ArrayList<String> list = getTable("Oxygen");
//        for (String s : list){
//            System.out.println(s);
//        }
        GetTableUtil getTableUtil = new GetTableUtil();
        String value = getTableUtil.getName("水温");
        System.out.println("myProperty = " + value);
    }
}
